
/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <ohaudio/native_audiocapturer.h>
#include <ohaudio/native_audiostreambuilder.h>

typedef int32_t                                         ma_aaudio_result_t;
typedef int32_t                                         ma_aaudio_direction_t;
typedef int32_t                                         ma_aaudio_sharing_mode_t;
typedef int32_t                                         ma_aaudio_format_t;
typedef int32_t                                         ma_aaudio_stream_state_t;
typedef int32_t                                         ma_aaudio_performance_mode_t;
typedef int32_t                                         ma_aaudio_usage_t;
typedef int32_t                                         ma_aaudio_content_type_t;
typedef int32_t                                         ma_aaudio_input_preset_t;
typedef int32_t                                         ma_aaudio_allowed_capture_policy_t;
typedef int32_t                                         ma_aaudio_data_callback_result_t;

typedef struct ma_AAudioStreamBuilder_t ma_AAudioStreamBuilder;
typedef struct ma_AAudioStream_t ma_AAudioStream;
typedef ma_aaudio_data_callback_result_t (* ma_AAudioStream_dataCallback) (ma_AAudioStream* pStream, void* pUserData, void* pAudioData, int32_t numFrames);
typedef void                             (* ma_AAudioStream_errorCallback)(ma_AAudioStream *pStream, void *pUserData, ma_aaudio_result_t error);

#define MA_AAUDIO_UNSPECIFIED                           0

/* Result codes. miniaudio only cares about the success code. */
#define MA_AAUDIO_OK                                    0

/* Directions. */
#define MA_AAUDIO_DIRECTION_OUTPUT                      0
#define MA_AAUDIO_DIRECTION_INPUT                       1

/* Sharing modes. */
#define MA_AAUDIO_SHARING_MODE_EXCLUSIVE                0
#define MA_AAUDIO_SHARING_MODE_SHARED                   1

/* Formats. */
#define MA_AAUDIO_FORMAT_PCM_I16                        1
#define MA_AAUDIO_FORMAT_PCM_FLOAT                      2

/* Stream states. */
#define MA_AAUDIO_STREAM_STATE_UNINITIALIZED            0
#define MA_AAUDIO_STREAM_STATE_UNKNOWN                  1
#define MA_AAUDIO_STREAM_STATE_OPEN                     2
#define MA_AAUDIO_STREAM_STATE_STARTING                 3
#define MA_AAUDIO_STREAM_STATE_STARTED                  4
#define MA_AAUDIO_STREAM_STATE_PAUSING                  5
#define MA_AAUDIO_STREAM_STATE_PAUSED                   6
#define MA_AAUDIO_STREAM_STATE_FLUSHING                 7
#define MA_AAUDIO_STREAM_STATE_FLUSHED                  8
#define MA_AAUDIO_STREAM_STATE_STOPPING                 9
#define MA_AAUDIO_STREAM_STATE_STOPPED                  10
#define MA_AAUDIO_STREAM_STATE_CLOSING                  11
#define MA_AAUDIO_STREAM_STATE_CLOSED                   12
#define MA_AAUDIO_STREAM_STATE_DISCONNECTED             13

/* Performance modes. */
#define MA_AAUDIO_PERFORMANCE_MODE_NONE                 10
#define MA_AAUDIO_PERFORMANCE_MODE_POWER_SAVING         11
#define MA_AAUDIO_PERFORMANCE_MODE_LOW_LATENCY          12

/* Usage types. */
#define MA_AAUDIO_USAGE_MEDIA                           1
#define MA_AAUDIO_USAGE_VOICE_COMMUNICATION             2
#define MA_AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING  3
#define MA_AAUDIO_USAGE_ALARM                           4
#define MA_AAUDIO_USAGE_NOTIFICATION                    5
#define MA_AAUDIO_USAGE_NOTIFICATION_RINGTONE           6
#define MA_AAUDIO_USAGE_NOTIFICATION_EVENT              10
#define MA_AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY        11
#define MA_AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE  12
#define MA_AAUDIO_USAGE_ASSISTANCE_SONIFICATION         13
#define MA_AAUDIO_USAGE_GAME                            14
#define MA_AAUDIO_USAGE_ASSISTANT                       16
#define MA_AAUDIO_SYSTEM_USAGE_EMERGENCY                1000
#define MA_AAUDIO_SYSTEM_USAGE_SAFETY                   1001
#define MA_AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS           1002
#define MA_AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT             1003

/* Content types. */
#define MA_AAUDIO_CONTENT_TYPE_SPEECH                   1
#define MA_AAUDIO_CONTENT_TYPE_MUSIC                    2
#define MA_AAUDIO_CONTENT_TYPE_MOVIE                    3
#define MA_AAUDIO_CONTENT_TYPE_SONIFICATION             4

/* Input presets. */
#define MA_AAUDIO_INPUT_PRESET_GENERIC                  1
#define MA_AAUDIO_INPUT_PRESET_CAMCORDER                5
#define MA_AAUDIO_INPUT_PRESET_VOICE_RECOGNITION        6
#define MA_AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION      7
#define MA_AAUDIO_INPUT_PRESET_UNPROCESSED              9
#define MA_AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE        10

/* Allowed Capture Policies */
#define MA_AAUDIO_ALLOW_CAPTURE_BY_ALL                  1
#define MA_AAUDIO_ALLOW_CAPTURE_BY_SYSTEM               2
#define MA_AAUDIO_ALLOW_CAPTURE_BY_NONE                 3

/* Callback results. */
#define MA_AAUDIO_CALLBACK_RESULT_CONTINUE              0
#define MA_AAUDIO_CALLBACK_RESULT_STOP                  1


typedef ma_aaudio_data_callback_result_t (* ma_AAudioStream_dataCallback) (ma_AAudioStream* pStream, void* pUserData, void* pAudioData, int32_t numFrames);
typedef void                             (* ma_AAudioStream_errorCallback)(ma_AAudioStream *pStream, void *pUserData, ma_aaudio_result_t error);

extern "C" {
ma_aaudio_result_t AAudio_createStreamBuilder(ma_AAudioStreamBuilder** ppBuilder);
ma_aaudio_result_t AAudioStreamBuilder_delete(ma_AAudioStreamBuilder* pBuilder);
void AAudioStreamBuilder_setDeviceId(ma_AAudioStreamBuilder* pBuilder, int32_t deviceId);
void AAudioStreamBuilder_setDirection(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_direction_t direction);
void AAudioStreamBuilder_setSharingMode(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_sharing_mode_t sharingMode);
void AAudioStreamBuilder_setFormat(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_format_t format);
void AAudioStreamBuilder_setChannelCount(ma_AAudioStreamBuilder* pBuilder, int32_t channelCount);
void AAudioStreamBuilder_setSampleRate(ma_AAudioStreamBuilder* pBuilder, int32_t sampleRate);
void AAudioStreamBuilder_setBufferCapacityInFrames(ma_AAudioStreamBuilder* pBuilder, int32_t numFrames);
void AAudioStreamBuilder_setFramesPerDataCallback(ma_AAudioStreamBuilder* pBuilder, int32_t numFrames);
void AAudioStreamBuilder_setDataCallback(ma_AAudioStreamBuilder* pBuilder, ma_AAudioStream_dataCallback callback, void* pUserData);
void AAudioStreamBuilder_setErrorCallback(ma_AAudioStreamBuilder* pBuilder, ma_AAudioStream_errorCallback callback, void* pUserData);
void AAudioStreamBuilder_setPerformanceMode(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_performance_mode_t mode);
void AAudioStreamBuilder_setUsage(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_usage_t contentType);
void AAudioStreamBuilder_setContentType(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_content_type_t contentType);
void AAudioStreamBuilder_setInputPreset(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_input_preset_t inputPreset);
void AAudioStreamBuilder_setAllowedCapturePolicy(ma_AAudioStreamBuilder* pBuilder, ma_aaudio_allowed_capture_policy_t policy);
ma_aaudio_result_t AAudioStreamBuilder_openStream(ma_AAudioStreamBuilder* pBuilder, ma_AAudioStream** ppStream);
ma_aaudio_result_t AAudioStream_close(ma_AAudioStream* pStream);
ma_aaudio_stream_state_t AAudioStream_getState(ma_AAudioStream* pStream);
ma_aaudio_result_t AAudioStream_waitForStateChange(ma_AAudioStream* pStream, ma_aaudio_stream_state_t inputState, ma_aaudio_stream_state_t* pNextState, int64_t timeoutInNanoseconds);
ma_aaudio_format_t AAudioStream_getFormat(ma_AAudioStream* pStream);
int32_t AAudioStream_getChannelCount(ma_AAudioStream* pStream);
int32_t AAudioStream_getSampleRate(ma_AAudioStream* pStream);
int32_t AAudioStream_getBufferCapacityInFrames(ma_AAudioStream* pStream);
int32_t AAudioStream_getFramesPerDataCallback(ma_AAudioStream* pStream);
int32_t AAudioStream_getFramesPerBurst(ma_AAudioStream* pStream);
ma_aaudio_result_t AAudioStream_requestStart(ma_AAudioStream* pStream);
ma_aaudio_result_t AAudioStream_requestStop(ma_AAudioStream* pStream);
}